/**
 * 
 */
package com.thinkgsp.jxc.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgsp.jxc.model.RpContact;
import com.thinkgsp.jxc.persistence.RpContactMapper;
import com.thinkgsp.jxc.service.RpContactService;
import com.thinkgsp.util.PeriodUtil;

/**
 * @author evan
 *
 */
@Service("contactService")
@Transactional
public class RpContactServiceImpl extends GeneralServiceImpl implements
		RpContactService {

	@Autowired
	private RpContactMapper contactMapper;
	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpContactService#setRpContactMapper(com.thinkgsp.jxc.persistence.RpContactMapper)
	 */
	public void setRpContactMapper(RpContactMapper contactMapper) {
		this.contactMapper = contactMapper;
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpContactService#getRpContact(java.lang.Long)
	 */
	public RpContact getRpContact(Long id) {
		return contactMapper.getRpContact(id);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpContactService#getRpContactList(int, int, int, int, int)
	 */
	public List<RpContact> getRpContactList(int begYear, int begPeriod,
			int endYear, int endPeriod, int custId) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		List<RpContact> list = contactMapper.getRpContactList(begin, end, custId);
		BigDecimal endAmount = BigDecimal.ZERO;
		for(RpContact contact : list) {
			contact.setIncludeCheckTag(true);
			if(contact.getARecAmount() != null) {
				endAmount = endAmount.add(contact.getARecAmount());
			} 
			if(contact.getRecAmount() != null) {
				endAmount = endAmount.subtract(contact.getRecAmount());
			}
			if(contact.getAPayAmount() != null) {
				endAmount = endAmount.subtract(contact.getAPayAmount());
			}
			if(contact.getPayAmount() != null) {
				endAmount = endAmount.add(contact.getPayAmount());
			}
			contact.setEndAmount(endAmount);
		}
		return list;
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.RpContactService#getRpContactList(int, int, int, int, int)
	 */
	public List<RpContact> getRpContactListNotCheck(int begYear, int begPeriod,
			int endYear, int endPeriod, int custId) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		List<RpContact> list = contactMapper.getRpContactListNotCheck(begin, end, custId);
		BigDecimal endAmount = BigDecimal.ZERO;
		for(RpContact contact : list) {
			contact.setIncludeCheckTag(false);
//			if(contact.getARecAmountCheck() != null) {
//				endAmount = endAmount.add(contact.getARecAmountCheck());
//			}
//			if(contact.getRecAmountCheck() != null) {
//				endAmount = endAmount.subtract(contact.getRecAmountCheck());
//			}
//			if(contact.getAPayAmountCheck() != null) {
//				endAmount = endAmount.subtract(contact.getAPayAmountCheck());
//			}
//			if(contact.getPayAmountCheck() != null) {
//				endAmount = endAmount.add(contact.getPayAmountCheck());
//			}
			if(contact.getARecAmount() != null) {
				endAmount = endAmount.add(contact.getARecAmount());
			} 
			if(contact.getRecAmount() != null) {
				endAmount = endAmount.subtract(contact.getRecAmount());
			}
			if(contact.getAPayAmount() != null) {
				endAmount = endAmount.subtract(contact.getAPayAmount());
			}
			if(contact.getPayAmount() != null) {
				endAmount = endAmount.add(contact.getPayAmount());
			}
			contact.setEndAmount(endAmount);
		}
		
		return list;
	}
	public List<RpContact> getRpContactListForARec(int begYear, int begPeriod,
			int endYear, int endPeriod, int custId) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		List<RpContact> list = contactMapper.getRpContactListByDirect(begin, end, custId,1);
		if(list == null || list.size() == 0) return list;
		BigDecimal begAmount = BigDecimal.ZERO;
//		if(list.get(0).getEndAmount().compareTo(BigDecimal.ZERO) != 0) {
//			begAmount = list.get(0).getEndAmount();
//		}
		if(BigDecimal.ZERO.compareTo(list.get(0).getEndAmount()) != 0) {
			begAmount = list.get(0).getEndAmount();
		}
		BigDecimal arpAmount = BigDecimal.ZERO;
		BigDecimal rpAmount = BigDecimal.ZERO;
		for(RpContact contact : list) {
			if(contact.getTypeId() != 0) {
				if(contact.getArpAmount() == null) {
					arpAmount = BigDecimal.ZERO;
				} else {
					arpAmount = contact.getArpAmount();
				}
				if(contact.getRpAmount() == null) {
					rpAmount = BigDecimal.ZERO;
				} else {
					rpAmount = contact.getRpAmount();
				}
				begAmount = begAmount.add(arpAmount).subtract(rpAmount);
				contact.setEndAmount(begAmount);
			}
		}
		return list;
	}

	public List<RpContact> getRpContactListForAPay(int begYear, int begPeriod,
			int endYear, int endPeriod, int custId) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		List<RpContact> list = contactMapper.getRpContactListByDirect(begin, end, custId,-1);
		if(list == null || list.size() == 0) return list;
		BigDecimal begAmount = BigDecimal.ZERO;
		if(list.get(0).getEndAmount().compareTo(BigDecimal.ZERO) != 0) {
			begAmount = list.get(0).getEndAmount();
		}
		BigDecimal arpAmount = BigDecimal.ZERO;
		BigDecimal rpAmount = BigDecimal.ZERO;
		for(RpContact contact : list) {
			if(contact.getTypeId() != 0) {
				if(contact.getArpAmount() == null) {
					arpAmount = BigDecimal.ZERO;
				} else {
					arpAmount = contact.getArpAmount();
				}
				if(contact.getRpAmount() == null) {
					rpAmount = BigDecimal.ZERO;
				} else {
					rpAmount = contact.getRpAmount();
				}
				begAmount = begAmount.add(arpAmount).subtract(rpAmount);
				contact.setEndAmount(begAmount);
			}
		}
		return list;
	}

}
